package com.weiweiqin.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.weiweiqin.model.*;
import com.weiweiqin.service.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.weiweiqin.dao.OrderDao;
import com.weiweiqin.vo.common.CartVO;
import com.weiweiqin.vo.common.Page;

@Service
public class OrderServiceImpl implements OrderService {
	
	@Autowired
	private OrderDao orderDao;
	
	@Autowired
	private GoodsService goodsService;

	@Autowired
	private CustomerService customerService;
	
	@Autowired
	private ColorService colorService;

	@Autowired
	private PrizeService prizeService;

	@Autowired
	private IntegrallogService integrallogService;
	
	@Autowired
	private OrderGoodsItemService orderGoodsItemService;
	
	@Autowired
	private IpLimitService ipLimitService;
	
	public void update(Order order) {
		orderDao.update(order);
	}

	public Order get(int id) {
		return orderDao.get(id);
	}
	
	/**
	 * 新增订单
	 */
	@Transactional
	public void addDp(Order order, String province, String city, String area,
			String street,Map<String,Object> conditions) {
		String addr = province+city+area+street;
		order.setOuterid("E"+System.currentTimeMillis());
		order.setProvince(province); 
		order.setCity(city);
		order.setArea(area);
		order.setAddr(addr);
		orderDao.insert(order);
	}
	
	@Transactional
	public void addDpShop(Order order, String province, String city,
			String area, String street, int goodsId,int num,String color,String size,
			Map<String, Object> conditions) {
		String addr = province+city+area+street;
		order.setOuterid("E"+System.currentTimeMillis());
		order.setProvince(province); 
		order.setCity(city);
		order.setArea(area);
		order.setAddr(addr);
		order.setHost(order.getHost()+"单品");
		Goods goods = goodsService.get(goodsId);
		goods.setBuyNum(goods.getBuyNum()+1);
		goodsService.update(goods);
		if(null != goods){
			double nowPrice = goods.getNowPrice();
			nowPrice = nowPrice * num;
			BigDecimal big = new BigDecimal(nowPrice);
			order.setTotalPrice(big);
			orderDao.insert(order);
			OrderGoodsItem item = new OrderGoodsItem();
			item.setGoodsId(goodsId);
			item.setOrderId(order.getId());
			item.setColor(color);
			item.setSize(size);
			item.setNum(num);
			item.setMark(0);
			orderGoodsItemService.save(item);
		}
		
		
	}

	public List<Order> pageList(Page page, Map<String, Object> conditions) {
		return orderDao.pageList(page, conditions);
	}
	
	public List<Order> getByGoodsTitle(String title) {
		List<Order> list = new ArrayList<Order>();
		List<Goods> goodsList = goodsService.getByName(title);
		List<OrderGoodsItem> orderGoodsItemList = new ArrayList<OrderGoodsItem>();
		for (Goods goods : goodsList) {
			List<OrderGoodsItem> items = orderGoodsItemService.getOrderGoodsItemListByGoodsId(goods.getId());
			orderGoodsItemList.addAll(items);
		}
		for (OrderGoodsItem orderGoodsItem : orderGoodsItemList) {
			Order order = orderDao.get(orderGoodsItem.getOrderId());
			if(null != order){
				if(order.getStatus() == 6){
					list.add(order);
				}
			}
		}
		return list;
	}

	public int totalCount(Map<String, Object> conditions) {
		return orderDao.totalCount(conditions);
	}

	@Transactional
	public void saveByPrize(Order order, List<CartVO> goodsList, Prize prize) {
		order.setOuterid("E"+System.currentTimeMillis());
		//设置奖品已经使用
		prize.setStatus(2);
		prizeService.update(prize);
		order.setStatus(1);
		orderDao.insert(order);
		int addNum = 0;
		if(null != goodsList && !goodsList.isEmpty()){
			for (CartVO cartVO : goodsList) {
				if(null != cartVO){
					//计算购买商品数量
					addNum = addNum + cartVO.getNum();
					int goodsId = cartVO.getGoodsId();
					Goods goods = goodsService.get(goodsId);
					goods.setBuyNum(goods.getBuyNum()+cartVO.getNum());
					goodsService.update(goods);
					OrderGoodsItem item = new OrderGoodsItem();
					item.setGoodsId(goodsId);
					item.setOrderId(order.getId());
					item.setColor(cartVO.getColor());
					item.setSize(cartVO.getSize());
					item.setNum(cartVO.getNum());
					item.setMark(0);
					orderGoodsItemService.save(item);
				}
			}
		}
	}

	@Transactional
	public void saveByJF(Order order, List<CartVO> goodsList, Customer customer) {
		order.setOuterid("E"+System.currentTimeMillis());
		Integrallog integrallog = new Integrallog();
		integrallog.setCreateTime(new Date());
		integrallog.setCustomerId(order.getCustomerId());
		integrallog.setGoodsId(goodsList.get(0).getGoodsId());
		integrallog.setIntegral(goodsList.get(0).getGoods().getIntegral());
		integrallog.setType(3);
		integrallogService.save(integrallog);
		//更新用户积分
		customerService.update(customer);
		order.setStatus(1);
		orderDao.insert(order);
		int addNum = 0;
		if(null != goodsList && !goodsList.isEmpty()){
			for (CartVO cartVO : goodsList) {
				if(null != cartVO){
					//计算购买商品数量
					addNum = addNum + cartVO.getNum();
					int goodsId = cartVO.getGoodsId();
					Goods goods = goodsService.get(goodsId);
					goods.setBuyNum(goods.getBuyNum()+cartVO.getNum());
					goodsService.update(goods);
					OrderGoodsItem item = new OrderGoodsItem();
					item.setGoodsId(goodsId);
					item.setOrderId(order.getId());
					item.setColor(cartVO.getColor());
					item.setSize(cartVO.getSize());
					item.setNum(cartVO.getNum());
					item.setMark(0);
					orderGoodsItemService.save(item);
				}
			}
		}
	}

	@Transactional
	public void save(Order order, List<CartVO> goodsList) {
		order.setOuterid("E"+System.currentTimeMillis());
		order.setStatus(1);
		orderDao.insert(order);
		int addNum = 0;
		if(null != goodsList && !goodsList.isEmpty()){
			for (CartVO cartVO : goodsList) {
				if(null != cartVO){
					//计算购买商品数量
					addNum = addNum + cartVO.getNum();
					int goodsId = cartVO.getGoodsId();
					Goods goods = goodsService.get(goodsId);
					goods.setBuyNum(goods.getBuyNum()+cartVO.getNum());
					goodsService.update(goods);
					OrderGoodsItem item = new OrderGoodsItem();
					item.setGoodsId(goodsId);
					item.setOrderId(order.getId());
					item.setColor(cartVO.getColor());
					item.setSize(cartVO.getSize());
					item.setNum(cartVO.getNum());
					item.setMark(0);
					orderGoodsItemService.save(item);
				}
			}
		}
	}
	
	public List<Order> export(Map<String, Object> conditions){
		return orderDao.export(conditions);
	}

	public void del(int id) {
		orderDao.delete(id);
		
	}

	public List<Order> getOrderListByGoodsId(int goodsId) {
		return orderDao.getOrderListByGoodsId(goodsId);
	}


	public List<OrderGoodsItem> getOrderGoodsItemListById(int id) {
		return orderGoodsItemService.getOrderGoodsItemListByOrderId(id);
	}
	
	public List<CartVO> getCartVOList(int id){
		List<OrderGoodsItem> itemList = getOrderGoodsItemListById(id);
		List<CartVO> cartVOList = new ArrayList<CartVO>();
		if(null != itemList && !itemList.isEmpty()){
			for (OrderGoodsItem item : itemList) {
				int goodsId = item.getGoodsId();
				Goods goods = goodsService.get(goodsId);
				CartVO vo = new CartVO();
				vo.setGoods(goods);
				vo.setColor(item.getColor());
				vo.setSize(item.getSize());
				vo.setNum(item.getNum());
				Color color = colorService.getByColorNameAndGoods(item.getColor(), goodsId);
				if(null !=color){
					if(!StringUtils.isEmpty(color.getPic())){
						vo.setPic(color.getPic());
					}
				}
				cartVOList.add(vo);
			}
		}
		return cartVOList;
	}
	

	public Order getByOuterid(String outerid) {
		return orderDao.getByOuterid(outerid);
	}

	public List<Goods> getGoodsListByOrderId(int orderId) {
		// TODO Auto-generated method stub
		return null;
	}
	
	public int getWaitPayTotalCount(Map<String,Object> conditions) {
		return orderDao.getWaitPayTotalCount(conditions);
	}

	public List<Order> getProcessed1() {
		return orderDao.getProcessed1();
	}


}